package com.dwxt.sport.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.Page;
import com.dwxt.common.util.IDUtils;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.sport.dao.DeviceSceneMapper;
import com.dwxt.sport.dao.SceneMapper;
import com.dwxt.sport.entity.DeviceScene;
import com.dwxt.sport.entity.Scene;
import com.dwxt.sport.entity.WifeDevice;
import com.dwxt.sport.entity.vo.SceneVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SceneServiceAdmin {

    @Autowired
    private SceneMapper sceneMapper;

    @Autowired
    private DeviceSceneMapper deviceSceneMapper;

    @Autowired
    private DeviceServiceAdmin deviceServiceAdmin;

    public BaseResult selectScenePage(Page page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Example example = new Example(Scene.class);
        example.orderBy("createTime").desc();
        List<Scene> scenes = sceneMapper.selectByExample(example);
        PageInfo<Scene> pageInfo = new PageInfo<>(scenes);
        return BaseResult.ok(pageInfo);
    }

    public void createScene(Scene scene) {
        scene.setId(IDUtils.genItemId());
        sceneMapper.insertSelective(scene);
    }

    public void updateScene(Scene scene) {
        sceneMapper.updateByPrimaryKeySelective(scene);
    }

    public void deleteScene(String id) {
        sceneMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public void relateDevice(SceneVo sceneVo) {
        //删除以前的关联
        deleteRelateBySceneId(sceneVo.getId());

        //新增新的关联
        for (String deviceId : sceneVo.getDeviceIds()) {
            DeviceScene deviceScene = new DeviceScene();
            deviceScene.setId(IDUtils.genItemId());
            deviceScene.setSceneId(sceneVo.getId());
            deviceScene.setDeviceId(deviceId);
            deviceSceneMapper.insertSelective(deviceScene);
        }
    }

    private void deleteRelateBySceneId(String sceneId) {
        Example example = new Example(DeviceScene.class);
        example.createCriteria().andEqualTo("sceneId", sceneId);
        deviceSceneMapper.deleteByExample(example);
    }

    public BaseResult selectDetailById(String id) {
        Scene scene = sceneMapper.selectByPrimaryKey(id);
        scene.setCreateTime(null);
        //查询设备id
        List<String> deviceIds = selectDeviceIds(id);
        //查询设备详情
        List<WifeDevice> wifeDeviceList = deviceServiceAdmin.selectDeviceByIds(deviceIds);
        HashMap hashMap = JsonUtils.jsonToPojo(JsonUtils.objectToJson(scene), HashMap.class);
        hashMap.put("deviceList", wifeDeviceList);
        return BaseResult.ok(hashMap);
    }

    private List<String> selectDeviceIds(String id) {
        Example example = new Example(DeviceScene.class);
        example.createCriteria().andEqualTo("sceneId", id);
        return deviceSceneMapper.selectByExample(example).stream().map(DeviceScene::getDeviceId).collect(Collectors.toList());
    }
}
